#include "game.h"
#include "PlayerInput.h"
#include "EndGameCondition.h"


using namespace std;

//Constructor
CGame::CGame()
{
    m_eState = MENU;

	//We set SGameSetting 
	
	m_eState.m_uiRows = MAX_Rows;
	m_eState.m_uiColumns = MAX_COLUMNS;
	m_eState.m_uiLevel = MAX_LEVEL;
	m_eState.m_Tokens = MAX_TOKENS;
	

	m_players = NULL; 
	m_board = NULL;*/

	//Create EndGameConditions
	//m_pEndGameConditions.push_back(new CEndGameConditionsWin(m_sGameSettings.m_TokensToJoin));

	//Crete drawing in GameRenderer
	//m_pGameRenderer = new CGameRendererType();

	//Token hasn't been introduced
	//m_bTokenInput = false;
}


//Destructor
CGame::~CGame()
{
	delete m_board;
}


/*GAME_SETTINGS CGame::GetGameSettings()
{
	return false; m_GameSetting;
}*/


bool CGame::UpdateGameState()
{
    
    switch(m_eState){
        case MENU:
            Menu();
            break;
            
        case INIT:
            Init();
            break;
            
        case PAINT:
            Paint();
            break;
            
        case CHOOSE_X_Y:
            Choose_X_Y();
            break;
            
        case CHECK:
            Check();
            break;

        case CHANGE_TURN:
            Change_Turn();
            break;

        case UPDATE:
            Update();
            break;
            
		case END:
            End();
            break;

        case EXIT:
            Exit(); 
            return true;
            break;
            
        default:
            return true;
    }

    return false;    
} 


void CGame::Menu()
{
    MENU m_PlayerInput[1];

	m_PlayerInput[0].m_cOption = 'a';
	m_PlayerInput[0].m_sOptionText = "New Game";

	//char l_cOption = m_pGameRenderer->PaintMenu(l_sMenuItems, 1, ".:: Game Conect 4 ::.");

	switch(l_cOption)
	{
		case 'y':
			m_eState = INIT;
			break;

		case 'n':
			m_eState = EXIT;
			break;
	}
}

void CGame::Init()
{
	//Inicializamos el tablero y los jugadores
	if (InitBoard())
	{
		m_board = new CBoard(m_sGameSettings.m_eBoardSize);
		//m_pGameRenderer->Board(m_board);
		m_sGameMsg[0]->Board(m_board);

		if (InitPlayers())
		{
			//No se ha introducido token
			m_sToken = false;

			m_eState = PAINT;
		}
		else
		{
			m_eState = END;
		}
	}
	else
	{
		m_eState = END;
	}
}



void CGame::Paint()
{
/*	m_pGameRenderer->Clear();

	m_pGameRenderer->PaintLine(MAX_LINE, '_');
	cout << endl;

	m_pGameRenderer->CenterText(".:: GameConect 4 ::.", ' ');
	cout << endl;

	m_pGameRenderer->PaintLine(MAX_LINE, '_');
	cout << endl;

	m_pGameRenderer->PaintBoard();

	m_pGameRenderer->PaintLine(MAX_LINE, '_');
	cout << endl;

	if (m_bTokenInput)
	{
		m_eState = CHECK;
	}
	else
	{
		m_eState = INPUT;
	}*/
    
}



void CGame::Choose_X_Y()
{

}


void CGame::Check()
{
	/*bool l_bSearchEnd = false;

	for(m_EndGameConditions = m_pEndGameConditions.begin(); m_itEndGameConditions != m_pEndGameConditions.end(); ++m_itEndGameConditions)
	{
		l_bSearchEnd = (*m_itEndGameConditions)->CheckEndGameCondition();

		cout << (*m_itEndGameConditions)->Message();
	}

	if (l_bSearchEnd){
		m_eState = END;
	}
	else{
	    m_eState = CHANGE_TURN;
	}*/
}




void CGame::Change_Turn()
{
    m_eState = PAINT;
}

void CGame::Update()
{

}

void CGame::End()
{
    m_eState = MENU;
}


void CGame::Exit()
{
    cout << "Hasta luego!!!" << endl;
}
